<?php
include_once("common/header.html");
include_once("class/ProductInfo.php");
include_once("class/Member.php");
include_once("class/Ferry_gly.class.php");
$ac=$GLOBALS['_ac'];
$pw=$GLOBALS['_pw'];
$s = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));
$memberID = $_SESSION['sid'];
$m = new Member($s,$memberID);
$pid = abs($_REQUEST['pid']); // 产品ID
$aid = abs($_REQUEST['aid']);// 供应商ID
$p = new ProductInfo($s,$pid,$aid);

$p_info = $p->pInfo();//返回产品信息

if($p_info['p_type']=='H'){
	include 'productOrder_show.html';
	exit;
}
try{
	$p->saleCheck();//查看产品是否在售
}catch(ProductInfoException $e){
	getOut("home.html",$e->getMessage());
}

if($p_info['p_type']=='F'){// 套票
	include_once("/var/www/html/new/d/class/PackTicket.class.php");
	$lid = $p_info['lid'];
	$pack = new PackTicket($lid);
	$useDate = $pack->useDate();
	$mintime = substr($useDate['sDate'],0, 10);// 有效开始时间
	$maxtime = $useDate['mDate'] ? $useDate['mDate']:'2020-10-10';// 有效结束时间
	// 使用开始时间大于结束时间或查看的月份大于最大月份，套票无效
	$begintime = (substr($useDate['sDate'],0,10) > substr($useDate['eDate'],0,10)) ? -1:$mintime;//
	if($begintime==-1) getOut("home.html","无有效出行日期，请更换产品");
}
$begintime = $p->getMinValidTime($begintime);/*产品有效起始日期*/
if($begintime==-1) getOut("home.html","无有效出行日期，请更换产品");
if(!$p->supply($memberID)) getOut("home.html","无购买权限");
if($begintime<date('Y-m-d')) $begintime = date('Y-m-d');

// $jsprice = $p->jsPrice_n($begintime);//返回产品分销价
$xml = $s->Dynamic_Price_And_Storage($_SESSION['saccount'], $pid, $begintime, 1, 0, 1, $aid);
if($xml==105 || $xml==1065 || $xml==101) getOut($_SERVER['HTTP_REFERER'],"无有效出行日期，请更换产品");
$object = simpleXML_load_string($xml);
$jsprice = (int)$object->Rec->UUsprice;
//$storage = ((string)$object->Rec->UUstorage==-1) ? '不限':(string)$object->Rec->UUstorage;
$storage = $p->dayStorage($begintime);
$uprice = $p->uPrice_n($begintime);//返回产品零售价
$buynum = $p_info['buy_limit_low'] ? $p_info['buy_limit_low']:1;

echo $begintime;
// 如果是时间段，判断预定日是否在有效期内
if($p_info['order_start']!='' && $p_info['order_end']!=''){
	if($p_info['order_end']<$begintime) getOut($_SERVER['HTTP_REFERER'],"预定的产品已过期，请检查产品");
}












// if(is_array($jsprice)) getOut($_SERVER['HTTP_REFERER'],"无有效出行日期，请更换产品");
// 产品信息
$payMethod = $p_info['pay'];
$products=array();
$products[]=array(
	"pid"       => $pid,
	"id"        => "purchaseNum",
	"name"      => "tnum",
	"ttitle"    => $p_info['ttitle'].($p_info['v_time_limit']==0 ? '' : '(<span style="color:#F33">验票时间'.str_replace('|', '至', $p_info['v_time_limit']).'</span>)'),
	"buynum"    => $buynum,
	"jsprice"   => $jsprice,
	"uprice"    => $uprice,
	"tprice"    => $p_info['tprice'],
	"minTime"   => $begintime,
	"ddays"     => $p_info['ddays'],
	"buy_limit_low" => $p_info['buy_limit_low'],
	'buy_limit_up'  => $p_info['buy_limit_up'],
	'pay'       =>$p_info['pay'],
	'sendVoucher'=>$p_info['sendVoucher'],
	'order_start'=>$p_info['order_start'],
	'order_end'=>$p_info['order_end'],
    'reb'=>$p_info['reb'],
    'reb_type'=>$p_info['reb_type'],
    'storage'=>$storage,
);

if($p_info['p_type']=='F'){// 套票
	// 产品有效期
	$products[0]['valid'] = '';
}else{
	// 产品有效期
	$valid = _htmlValid($p_info);
	$products[0]['valid'] = $valid;
	if ( $p_info['p_type']!="F" && $p_info['v_time_limit']==0 && $p_info['sync_status']!=2) {
		$c_pros=$m->salePros($p_info['lid']);// 该景区下的其他门票
		foreach($c_pros as $row){
			if($row['sapply_did']!=$_REQUEST['aid'] && $row['apply_did']!=$_SESSION['sid']) {
                    continue;
			}
			if($row['order_start']!=$p_info['order_start']) continue;
			if($row['order_end']!=$p_info['order_end']) continue;
			if($row['ddays']!=$p_info['ddays']) continue;
			if($row['delaydays']!=$p_info['delaydays']) continue;
			if($row['order_limit']!=$p_info['order_limit']) continue;
			if($row['tourist_info']!=$p_info['tourist_info']) continue;
            /*
            *之前$c_pros里没有取tourist_info 值是Null
            *而$p_info里的tourist_info的值是字符串的'0'
            *两者不相等 因此联票都显示不出来
            */
			$c_pids[]=$row['pid'];
		}
		if (count($c_pids)) {
			foreach($c_pids as $c_pid){
				if($c_pid==$p_info['pid']) {
                    continue;
                }else{
                    $apro=new ProductInfo($s,$c_pid,$aid);
                    $storage1= $apro ->dayStorage($begintime);
                }
				try{
					// $c_jsprice=$apro->jsPrice($memberID,$begintime);
					// $c_uprice=$apro->uPrice($memberID,$begintime);
					
					$c_jsprice=$apro->jsPrice_n($begintime);
					if($c_jsprice==-1 || is_array($c_jsprice)) continue;
					$c_uprice=$apro->uPrice_n($begintime);
					if($c_uprice==-1) continue;
					$c_pro=$apro->pInfo();
					
					if($c_pro['v_time_limit'] != 0 || $c_pro['sync_status']==2) continue;//验票时间是否不限
					if($c_pro['pay'] !=$payMethod) continue;
					// 
					if($products[0]['sendVoucher']!=$c_pro['sendVoucher'])continue;
					$time=strtotime($begintime)+($c_pro['ddays']-$p_info['ddays'])*24*3600;
					$minTime=date("Y-m-d",$time);
					$products[]=array(
                        "pid"=>$c_pro['pid'],
                        "id"=>$c_pro['id'],
                        "name"=>"c_pids[".$c_pid."]",
                        "ttitle"=>$c_pro['ttitle'],
                        "buynum"=>0,
                        "jsprice"=>$c_jsprice,
                        "uprice"=>$c_uprice,
                        "tprice"=>$c_pro['tprice'],
                        "minTime"=>$minTime,
                        "buy_limit_low"=>$c_pro['buy_limit_low'],
                        'buy_limit_up'=>$c_pro['buy_limit_up'],
                        'valid'=>_htmlValid($c_pro),
                        'order_start'=>$c_pro['order_start'],
                        'order_end'=>$c_pro['order_end'],
                        'reb'=>$c_pro['reb'],
                        'reb_type'=>$c_pro['reb_type'],
                        'storage'=>$storage1,
                    );
				}catch(Exception $e){
					//结算价未设置
				}
			}
		}
	}
	
}
//供应商信息
$str = "select dname,qq,mobile,cname from pft_member where id=".$p_info['sapply_did']." limit 1";
$GLOBALS['le']->query($str);
$a_info = $GLOBALS['le']->fetch_assoc();


//产品说明
$timeTitle=($p_info['ddays']==0)?"有效期内，可预订当天门票":"有效期内，需提前".($p_info['ddays'])."天预订";
$unitT=$p_info['p_type']=="C"?"间":"张";
$tips=$p_info['ddays']>0?"需提前".$p_info['ddays']."天预订，":"可预订当日票，";
$tips.=$p_info['buy_limit_low']>0?$p_info['buy_limit_low'].$unitT."起订，":"1".$unitT."起订，";
if($p_info['p_type']=="A"){
	$tips.=($p_info['delaydays']>0)?"游玩日期起".$p_info['delaydays']."天内有效，":"游玩日期当日有效，";
}
$tips.=($p_info['status']!=0)?"<i>".($p_info['mdays']!=0?"自动":"人工")."确认</i>后生效，":"";
$tips=$tips.$p_info['notes'];


//金额
$v['tmoney'] = $m->tmoney($p_info['sapply_did']);//查询会员账户可用授信额度
$v['money'] = $m->money();//查询会员账户余额
$cinfos = $m->cinfos(-1);//查询常用联系人

if($v['tmoney']>$jsprice*$tnum){
	$paymodeCheck0="";
	$paymodeCheck1="";
	$paymodeCheck2="checked";
}elseif($v['money']>$jsprice*$tnum){
	$paymodeCheck0="checked";
	$paymodeCheck1="";
	$paymodeCheck2="";
}else{
	//$paymodeCheck0="checked";//暂时关闭支付宝
	$paymodeCheck0="";
	$paymodeCheck1="checked";
	$paymodeCheck2="";
}

function getOut($url,$msg=null){
	$js="<script>";
	if($msg){ $js.="alert('$msg');" ;}
	$js.="window.location='$url'</script>";
	exit($js);
}
$token = '6f0c6c49daa58b9d802189ac932dcd65';
$appid = base64_encode('www.12301.cc');

function _htmlValid($p_info){
	if($p_info['delaydays']>0){
		if($p_info['delaytype']==0) $valid = '游玩日期(含)'.$p_info['delaydays'].'天内有效';
		if($p_info['delaytype']==1) $valid = '下单日期(含)'.$p_info['delaydays'].'天内有效';
	}
	if($p_info['order_start']=='' && $p_info['delaydays'] == 0){
		$valid = '订单当天有效';
	}
	
	if($p_info['order_start']!='' && $p_info['order_end']!='' ){
		$valid = '有效日期从'.$p_info['order_start'].' 至 '.$p_info['order_end'];
	}elseif($p_info['order_start']=='' && $p_info['order_end']!=''){
		$valid = '截止'.$p_info['order_end'];
	}
	
	
	return $valid;
}

if($p_info['p_type']!='C'){
    $land_f_info = $p_info;
}

//鼓浪屿轮渡查询票类余票
$gly_client = new Ferry_gly();
$selectTxt='<option>--------请选择---------</option>';
$selectTxt.='<option>开船时间&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp余票</option>';

if($_GET['pzp']){
	$str_ticket = "SELECT qt FROM uu_land WHERE id=".$p_info['lid']." limit 1";
	$GLOBALS['le']->query($str_ticket);
	$GLOBALS['le']->fetch_assoc();
	$gly_number = $GLOBALS['le']->f('qt');
	$getDailyFlight = $gly_client->getDailyFlight($gly_number);//查询航线信息
	$getDailyFlight = simpleXML_load_string($getDailyFlight);
	foreach($getDailyFlight->content->list as $k){
		foreach($k->DailyFlight as $v){
			$offLineFreeCount = $v->offLineFreeCount;
			$departTime = substr((string)$v->departTime,11,8);
			$selectTxt.='<option value="'.$departTime.'|'.$offLineFreeCount.'" name="ferry_data" >'.$departTime.'&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp'.$offLineFreeCount.'</option>';
		}
	}
}
$date_format = array('周日','周一','周二','周三','周四','周五','周六');

// print_r($products);

// 判断是否同一供应链
if(count($products)>1 && $p_info['apply_did']!=$memberID){

	if($p_info['apply_did']==$aid){
		$sql = "select pids from pft_product_sale_list where fid=6610 and aid=27116 limit 1";
		$GLOBALS['le']->query($sql);
		$GLOBALS['le']->fetch_assoc();
		$pids = $GLOBALS['le']->f('pids');
		if($pids!='A'){
			$pids_a = explode(',', $pids);
			foreach($products as $key=>$row){
				if(in_array($row['pid'], $pids_a)) unset($products[$key]);
			}
		}
	}else{
		$chain = '';
		foreach($products as $key=>$row){
			$sql = "select aids from pft_p_apply_evolute where pid={$row['pid']} and fid=$memberID and sid=$aid limit 1";
			$GLOBALS['le']->query($sql);
			$GLOBALS['le']->fetch_assoc();
			$db_aids = $GLOBALS['le']->f('aids');
			echo $db_aids;
			if($key==0) $chain = $db_aids;
			if($chain!=$db_aids) unset($products[$key]);
		}
	}
}

include("tpl/productOrder_dev.tpl");
include("common/footer.html");